Add env and env_file support to machine configuration#498
Conversation
🦋 Changeset detectedLatest commit: a6f9068 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Coverage Report
📁 File Coverage (19 files)
|
| envMap[variable] = ""; | ||
| } else { | ||
| envMap[variable.slice(0, eq)] = variable.slice(eq + 1); | ||
| } |
There was a problem hiding this comment.
@tuler,
These slices are odd. Could it be done by using a destructuring based on the assumptions of the variable content?
e.g.
const [key, value=""] = variable.split("=");
envMap[key] = value;|
The PR description is strongly on the "how", but I don't understand clearly the "why". Does it help solve an issue Lyno opened? |
Allow injecting environment variables into the Cartesi Machine build beyond the Dockerfile ENV controlled by use_docker_env: - env: an inline table of key/value pairs in [machine.env] - env_file: a path to a .env file Precedence (lowest to highest): docker image ENV (when use_docker_env is enabled), env_file, then the env table. Also fixes a latent bug where env values containing "=" were truncated at the first "=". Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016vnD51QAGwAvqaZuEZJypr
683a9eb to
a6f9068
Compare
Added a motivation section to the PR description. |
|
|
||
| # [machine.env] # optional. environment variables injected into the machine | ||
| # FOO = "bar" | ||
| # LOG_LEVEL = "debug" |
There was a problem hiding this comment.
@tuler,
I am curious and just trying to understand how to use it. Is the idea to plug in anything from the environment variable, like arbitrary values that only make sense to what is running inside, or is it to change the behaviour of the machine, like the rollups_node env vars? If the latter would be good to have a list of behaviour-changing ones as they're specific and "reserved" in some sense.
PS: When I build dapps, the default configurations so far work for me, so I am trying to grasp the use. Even though you mentioned the motivation as the workflows and tests on different nets (dev, test ,main), it is not clear to me what these cases are; I mean what users are actually doing.
There was a problem hiding this comment.
it's for any arbitrary env var the application may use to configure the application logic.
examples for a financial application:
COLLATERAL_TOKEN_ADDRESS
PRICE_FEED_ADDRESS
Motivation
Environment variables are typically used to differentiate between multiple build or test scenarios. One classic examples are testnet vs devnet vs mainnet, but there may be other scenarios.
Currently the build process only supports environment variables defined inside the Dockerfile used by the cartesi build process. But the same variables defined in the Dockerfile may be necessary outside the cartesi build process. For example in a host build process that is not related to the cartesi build process at all. So in this case there is a duplication of environment variables values, which hurts maintenance.
By having
.envfiles support we can maintain a single source of truth for environment variables and use it in both the cartesi build workflow and other workflows.Summary
This PR adds support for injecting environment variables into the Cartesi Machine through two new configuration options in the
[machine]section ofcartesi.toml: anenvtable for inline key/value pairs and anenv_fileoption pointing to a.envfile.Key Changes
env(Record<string, string>) andenvFile(optional string) toMachineConfigparseStringRecord()to parse TOML tables into environment variable records, with automatic coercion of non-string scalar values (numbers, booleans) to strings.envfile parsing: AddedparseEnvFile()function that parses.envfiles with support for:#commentsexportprefix=signs in valuesbootMachine()to merge environment variables with proper precedence (lowest to highest):ENV(whenuse_docker_envis enabled)env_fileenvtableInvalidEnvErrorexception class for invalid env configurationsImplementation Details
cartesi-machine, allowing later sources to override earlier ones.envfile parser is robust and handles common.envfile formatsenvtable are automatically converted to strings since environment variables are always stringshttps://claude.ai/code/session_016vnD51QAGwAvqaZuEZJypr